package cn.buk.tms.dto;

import cn.buk.common.util.DecimalUtil;
import cn.buk.tms.common.dto.CustomerDto;
import cn.buk.tms.common.dto.PaymentMethodDto;
import com.fasterxml.jackson.annotation.JsonFormat;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 统计报表的内容
 *
 * @author yfdai
 */
public class ReportStatisticDto {

	/**
	 * 0-机票
	 */
	private int objectType;

	/**
	 * 没有散客，只有月结客户
	 */
	private CustomerDto customer;

	/**
	 * 对应customer.id, 方便统计时只获取到了customerId时的情况
	 */
	private Integer customerId;

	/**
	 * 客户代码，如果是散客，需要用此客户代码来找到对应的客户
	 */
	private String customerCode;

	private SupplierDto supplier;

	private PaymentMethodDto payMethod;

	private String op1;

	private String op2;

	/**
	 * 销售员的中文名称
	 */
	private String salesman;

	private String carrier;

	private int count;

	/**
	 * 日期
	 */
	@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
	private Date opDate;

	// 未支付
	private double unpaidTotalAmount;

	private double unpaidTotalCost;

	private double unpaidTotalProfit;

	// 已支付
	private double paidTotalAmount;

	private double paidTotalCost;

	private double paidTotalProfit;

	// 总的
	private double totalAmount;

	private double totalCost;

	private double totalProfit;

	private List<ReportStatSubItemDto> items;

	public static StringBuilder exportContent(final int index, final String name, ReportStatisticDto dto) {
		StringBuilder sb = new StringBuilder();
		sb.append(index).append(",");

		sb.append(name).append(",");
		sb.append(",");
		sb.append(dto.getCount()).append(",");
		sb.append(dto.getTotalAmount()).append(",");
		sb.append(dto.getTotalCost()).append(",");
		sb.append(dto.getUnpaidTotalCost()).append(",");
		sb.append(dto.getTotalProfit()).append(",");
		sb.append("\r");

		for(ReportStatSubItemDto item: dto.getItems()) {
			sb.append(",");
			sb.append(",");
			sb.append(item.getBillTypeName()).append(",");
			sb.append(item.getCount()).append(",");
			sb.append(item.getTotalAmount()).append(",");
			sb.append(item.getTotalCost()).append(",");
			sb.append(",");
			sb.append(item.getTotalProfit()).append(",");
			sb.append("\r");

		}

		return sb;
	}


	public void addItemByCustomerName(ReportStatisticDto dto) {
		final String customerName = dto.getCustomer() == null ? "" : dto.getCustomer().getName();

		ReportStatSubItemDto item = null;
		for (ReportStatSubItemDto itemDto : this.getItems()) {
			if (itemDto.getCustomerName().equalsIgnoreCase(customerName)) {
				item = itemDto;
				break;
			}
		}

		if (item == null) {
			item = new ReportStatSubItemDto();
			item.setCustomerName(customerName);

			this.getItems().add(item);
		}

		item.setCount(item.getCount() + dto.getCount());

		item.setTotalAmount(item.getTotalAmount() + dto.getTotalAmount());
		item.setTotalCost(item.getTotalCost() + dto.getTotalCost());
		item.setTotalProfit(item.getTotalProfit() + dto.getTotalProfit());

		this.items = this.items.stream()
				.sorted(Comparator.comparing(ReportStatSubItemDto::getCount).reversed())
				.collect(Collectors.toList());
	}

	public int getObjectType() {
		return objectType;
	}

	public void setObjectType(int objectType) {
		this.objectType = objectType;
	}

	public CustomerDto getCustomer() {
		return customer;
	}

	public void setCustomer(CustomerDto customer) {
		this.customer = customer;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public double getTotalAmount() {
		return totalAmount;
	}

	public void setTotalAmount(double totalAmount) {
		this.totalAmount = DecimalUtil.keepScale2(totalAmount);
	}

	public double getTotalCost() {
		return totalCost;
	}

	public void setTotalCost(double totalCost) {
		this.totalCost = DecimalUtil.keepScale2(totalCost);
	}

	public double getTotalProfit() {
		return totalProfit;
	}

	public void setTotalProfit(double totalProfit) {
		this.totalProfit = DecimalUtil.keepScale2(totalProfit);
	}

	public String getOp1() {
		return op1;
	}

	public void setOp1(String op1) {
		this.op1 = op1;
	}

	public String getOp2() {
		return op2;
	}

	public void setOp2(String op2) {
		this.op2 = op2;
	}

	public SupplierDto getSupplier() {
		return supplier;
	}

	public void setSupplier(SupplierDto supplier) {
		this.supplier = supplier;
	}

	public String getSalesman() {
		return salesman;
	}

	public void setSalesman(String salesman) {
		this.salesman = salesman;
	}

	public double getUnpaidTotalAmount() {
		return unpaidTotalAmount;
	}

	public void setUnpaidTotalAmount(double unpaidTotalAmount) {
		this.unpaidTotalAmount = DecimalUtil.keepScale2(unpaidTotalAmount);
	}

	public double getUnpaidTotalCost() {
		return unpaidTotalCost;
	}

	public void setUnpaidTotalCost(double unpaidTotalCost) {
		this.unpaidTotalCost = DecimalUtil.keepScale2(unpaidTotalCost);
	}

	public double getUnpaidTotalProfit() {
		return unpaidTotalProfit;
	}

	public void setUnpaidTotalProfit(double unpaidTotalProfit) {
		this.unpaidTotalProfit = DecimalUtil.keepScale2(unpaidTotalProfit);
	}

	public double getPaidTotalCost() {
		return paidTotalCost;
	}

	public void setPaidTotalCost(double paidTotalCost) {
		this.paidTotalCost = DecimalUtil.keepScale2(paidTotalCost);
	}

	public double getPaidTotalProfit() {
		return paidTotalProfit;
	}

	public void setPaidTotalProfit(double paidTotalProfit) {
		this.paidTotalProfit = DecimalUtil.keepScale2(paidTotalProfit);
	}

	public double getPaidTotalAmount() {
		return paidTotalAmount;
	}

	public void setPaidTotalAmount(double paidTotalAmount) {
		this.paidTotalAmount = DecimalUtil.keepScale2(paidTotalAmount);
	}

	public List<ReportStatSubItemDto> getItems() {
		if (items == null) {
			items = new ArrayList<>();
		}
		return items;
	}

	public void setItems(List<ReportStatSubItemDto> items) {
		this.items = items;
	}


	public String getCustomerCode() {
		return customerCode;
	}

	public void setCustomerCode(String customerCode) {
		this.customerCode = customerCode;
	}

	public String getCarrier() {
		return carrier;
	}

	public void setCarrier(String carrier) {
		this.carrier = carrier;
	}

	public PaymentMethodDto getPayMethod() {
		return payMethod;
	}

	public void setPayMethod(PaymentMethodDto payMethod) {
		this.payMethod = payMethod;
	}

	public Date getOpDate() {
		return opDate;
	}

	public void setOpDate(Date opDate) {
		this.opDate = opDate;
	}

	public int getCustomerId() {
		return customerId == null ? 0 : customerId;
	}

	public void setCustomerId(Integer customerId) {
		this.customerId = customerId;
	}
}
